
<?php

    @include_once("./conn.php");
    @header("Content-Type:text/json;charset=utf-8");   // 将php的运行结果以 json数据输出到页面

    $key = $_GET["key"];
    $orderCol = $_GET["orderCol"];      //排序的列名  id  goodsId goodsName goodsPrice
    $orderType = $_GET["orderType"];   //排序的方式 asc desc

    $pageIndex = $_GET["pageIndex"];   //第几页
    $showNum = $_GET["showNum"];    //显示多少条

    // limit  m,n  跳过m条,显示n条

    // 第1页  [0,5]    跳过0条 显示五条    [0,5]
    // 第2页  [6,10]   跳过5条 显示五条    [5,5]
    // 第3页  [11,15]   跳过10条 显示五条  [10,5]

    // 第n页            [($pageIndex-1)*$showNum,$showNum]

    
    $searchAll = "select id,goodsId,goodsName,goodsPrice,bigPicList from `goodslist` where goodsName like '%$key%' order by $orderCol $orderType";
    
    $result = mysqli_query($conn,$searchAll);
    // print_r($result);
    
    $count = $result->num_rows;
    $maxPage = ceil($count / $showNum);   //$count总数量 /  $showNum  => 最大页码


    $pageIndex = $pageIndex <1 ? 1 : $pageIndex;  // 最小值的临界值
    $pageIndex = $pageIndex >$maxPage ? $maxPage : $pageIndex;  // 最大值的临界值

    // echo $pageIndex;
    

    $skipNum =($pageIndex-1)*$showNum;


    $search = "select id,goodsId,goodsName,goodsPrice,bigPicList from `goodslist` where goodsName like '%$key%' order by $orderCol $orderType limit $skipNum,$showNum";

    $result = mysqli_query($conn,$search);

    $all = array();
    while($item = mysqli_fetch_assoc($result)){

        $str = $item["bigPicList"];   //字符串(多个图片以逗号分隔)
        $arr = explode(",",$str);    // 拆分成数组 
        $item["bigPicList"] = $arr[0];   //默认返回第一个

        array_push($all,$item);
    }

    echo json_encode($all);




?>